//
//  NSMAssetPickerCell.m
//  NSMKit
//
//  Created by nsm on 2019/3/4.
//  Copyright © 2019 nani. All rights reserved.
//

#import "NSMAssetPickerCell.h"
//#import "NSMKit.h"
#import "NSMAssetsManager.h"
//#import <YYKit.h>
//#import <MyLinearLayout.h>

@interface NSMAssetPickerCell ()
@property (nonatomic, strong) NSMImageView *assetImageView;
@property (nonatomic, strong) CAGradientLayer *shadowLayer;
@property (nonatomic, strong) NSMImageView *descIcon;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) MyLinearLayout *descLayout;
@end

@implementation NSMAssetPickerCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {
    _assetImageView = NSMImageView.new;
    [self.contentView addSubview:self.assetImageView];
    
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.colors = @[(id)RGBAColor(0, 0, 0, 0).CGColor, (id)RGBAColor(0, 0, 0, 0.6).CGColor];
    [self.contentView.layer addSublayer:layer];
    _shadowLayer = layer;
    
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    layout.myBottom = 0;
    layout.myHorzMargin = 0;
    layout.wrapContentHeight = YES;
    layout.padding = UIEdgeInsetsMake(4, 7, 4, 7);
    layout.subviewHSpace = 4;
    layout.gravity = MyGravity_Vert_Center;
    [self.contentView addSubview:layout];
    _descLayout = layout;
    
    NSMImageView *icon = NSMImageView.new;
    icon.size = CGSizeMake(14, 14);
    icon.contentMode = UIViewContentModeScaleAspectFit;
    icon.tintColor = UIColor.whiteColor;
    [layout addSubview:icon];
    _descIcon = icon;
    
    UILabel *label = UILabel.new;
    label.weight = 1;
    label.wrapContentHeight = YES;
    label.font = CustomFont(12);
    label.textColor = UIColor.whiteColor;
    label.textAlignment = NSTextAlignmentRight;
    [layout addSubview:label];
    _descLabel = label;
}

- (void)setAsset:(id)obj {
    if (![obj isKindOfClass:NSMAsset.class]) return;
    
    PHAsset *asset = ((NSMAsset *)obj).asset;
    @nsm_weak(self);
    [_assetsManager fecthThumbnailImageForAsset:asset size:self.contentView.size completion:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        @nsm_strong(self);
        self.assetImageView.image = result;
    }];
    
    BOOL isVideo = (asset.mediaType == PHAssetMediaTypeVideo);
    self.shadowLayer.hidden = !isVideo;
    self.descLayout.myVisibility = isVideo? MyVisibility_Visible: MyVisibility_Gone;
    
    self.descIcon.image = TemplateImage(@"nsm_mark_video");
    NSUInteger duration = (long)asset.duration;
    NSUInteger min = duration / 60;
    NSUInteger sec = duration % 60;
    self.descLabel.text = FormatString(@"%02ld:%02ld", (long)min, (long)sec);
}

#pragma mark - setter/getter
- (NSMImageView *)assetImageView {
    if (!_assetImageView) {
        _assetImageView = NSMImageView.new;
    }
    return _assetImageView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.assetImageView.frame = self.contentView.bounds;
    self.shadowLayer.frame = CGRectMake(0,
                                        self.contentView.height/2,
                                        self.contentView.width,
                                        self.contentView.height/2);
}

@end
